package com.max.sportmanage.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.management.relation.Role;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @program: sport-manage-java
 * @description: 用户实体
 * @author: max-qaq
 * @create: 2021-10-19 10:59
 **/
@Data
public class SysUser implements UserDetails {
    private Long id;

    private String userName;

    private String password;

    private String nickName;

    private Integer sex;

    private String avatar;

    private String address;

    private String openId;

    private boolean status;

    private boolean admin;

    private String phoneNumber;

    private List<SysRole> roles;

    /**
     * 用户对应的菜单列表
     */
    private List<SysMenu> menus;

    /**
     * 用户对应的权限
     */
    private List<SysPermission> permissions;

    /**
     * 权限数据
     * @return
     */
    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorityList = new ArrayList<>();
        if (null != roles && roles.size() > 0){
            roles.forEach(item -> {
                authorityList.add(new SimpleGrantedAuthority("ROLE_" + item.getCode()));
            });
        }
        if (null != permissions && permissions.size() > 0){
            permissions.forEach(item -> authorityList.add(new SimpleGrantedAuthority(item.getCode())));
        }
        return authorityList;
    }

    /**
     * 用户名
     * @return
     */
    @Override
    @JsonIgnore
    public String getUsername() {
        return userName;
    }

    /**
     * 账号是否过期
     * @return
     */
    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return false;
    }

    /**
     * 账号是否被锁定
     * @return
     */
    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return false;
    }


    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return false;
    }

    /**
     * 是否被禁用
     * @return
     */
    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return status;
    }
}
